package com.cmwa.solution.business.customerEquity.prdFeeYield.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.customerEquity.prdFeeYield.dao.PrdFeeYieldDao;
import com.cmwa.solution.business.customerEquity.prdFeeYield.entity.FeeYieldVo;
import com.cmwa.solution.business.customerEquity.prdFeeYield.entity.PrdFeeYieldVo;
import com.cmwa.solution.business.customerEquity.prdFeeYield.service.PrdFeeYieldExportService;
import com.cmwa.solution.business.customerEquity.prdFeeYield.service.PrdFeeYieldService;

@Service
public class PrdFeeYieldExportServiceImpl implements PrdFeeYieldExportService{

	@Resource
	private PrdFeeYieldDao prdFeeYieldDao;
	@Resource
	private PrdFeeYieldService prdFeeYieldService;
	
	@Override
	public void export(HttpServletResponse response, SearchParam sp) throws IOException {
		// TODO Auto-generated method stub
		
		//一层列表集合
		List<PrdFeeYieldVo> oneList  = prdFeeYieldService.prdFeeYieldList(sp);
		
		//二层列表集合
		List<FeeYieldVo> twoList  = new ArrayList<FeeYieldVo>();
		
		for(int i=0;i<oneList.size();i++){
			PrdFeeYieldVo prdFeeYieldVo = oneList.get(i);
			sp.getSp().put("subPrdCode", prdFeeYieldVo.getSubPrdCode());
			
			//获得费用集合
			List<FeeYieldVo> feeList = prdFeeYieldService.feeYieldList(sp);
			
			if(feeList != null && feeList.size()>0) {
				twoList.addAll(feeList);
			}
		}
		
		exportData(response,oneList,twoList);
	}

	/**
	 * 导出
	 * @throws IOException 
	 */
	public void exportData(HttpServletResponse response,
			List<PrdFeeYieldVo> oneList,List<FeeYieldVo> twoList) throws IOException{
		
		String curDateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());

		HSSFWorkbook wb = new HSSFWorkbook();
		
		// 样式
		HSSFCellStyle titleStyle = wb.createCellStyle();
		//titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
		HSSFFont titleFont = wb.createFont(); // 创建字体
		titleFont.setFontName("宋体"); // 黑体
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 加粗
		titleStyle.setFont(titleFont);
		HSSFSheet sheet1 = wb.createSheet("基本信息");
		HSSFSheet sheet2 = wb.createSheet("支付信息");
		
		sheet1.setColumnWidth(0, 20 * 256);
		sheet1.setColumnWidth(1, 20 * 256);
		sheet1.setColumnWidth(2, 20 * 256);
		sheet1.setColumnWidth(3, 20 * 256);
		sheet1.setColumnWidth(4, 20 * 256);
		sheet1.setColumnWidth(5, 20 * 256);

		HSSFRow row1 = sheet1.createRow(0);// 生成头部信息
		
		String[] titleRow1 = new String[] {"产品名称", "产品代码","份额",
				"基金代码","预付金额","实付金额"};

		for (int i = 0; i < titleRow1.length; i++) {
			HSSFCell cell = row1.createCell(i, Cell.CELL_TYPE_STRING);
			cell.setCellValue(titleRow1[i]);
			cell.setCellStyle(titleStyle);
		}

		//基本信息
		for (int i = 0; i < oneList.size(); i++) {

			PrdFeeYieldVo dto = oneList.get(i);
			HSSFRow rowData = sheet1.createRow(i + 1);

			HSSFCell cell = null;
			
			cell = rowData.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getPrjsName());
			cell = rowData.createCell(1, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getOutCode());
			cell = rowData.createCell(2, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getSubFundsName());
			cell = rowData.createCell(3, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getFundCode());
			cell = rowData.createCell(4, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getExpectPayAmount());
			cell = rowData.createCell(5, Cell.CELL_TYPE_STRING);
            cell.setCellValue(dto.getBnsAmt());
			
		}
		
		sheet2.setColumnWidth(0, 20 * 256);
		sheet2.setColumnWidth(1, 20 * 256);
		sheet2.setColumnWidth(2, 20 * 256);
		sheet2.setColumnWidth(3, 20 * 256);
		sheet2.setColumnWidth(4, 20 * 256);
		sheet2.setColumnWidth(5, 20 * 256);
		sheet2.setColumnWidth(6, 20 * 256);
		sheet2.setColumnWidth(7, 20 * 256);

		HSSFRow row2 = sheet2.createRow(0);// 生成头部信息
		
		String[] titleRow2 = new String[] {"份额","基金代码","名称","类型","预计支付日","预计支付金额",
				"实际支付日","实际支付金额"};

		for (int i = 0; i < titleRow2.length; i++) {
			HSSFCell cell = row2.createCell(i, Cell.CELL_TYPE_STRING);
			cell.setCellValue(titleRow2[i]);
			cell.setCellStyle(titleStyle);
		}

		//基本信息
		for (int i = 0; i < twoList.size(); i++) {

			FeeYieldVo dto = twoList.get(i);
			HSSFRow rowData = sheet2.createRow(i + 1);

			HSSFCell cell = null;
			
			cell = rowData.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getSubFundsName());
			cell = rowData.createCell(1, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getFundCode());
			cell = rowData.createCell(2, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getFeeName());
			cell = rowData.createCell(3, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getOldFeeName());
			cell = rowData.createCell(4, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getExpectPayDate());
			cell = rowData.createCell(5, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getExpectPayAmount());
			cell = rowData.createCell(6, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getBnsDate());
			cell = rowData.createCell(7, Cell.CELL_TYPE_STRING);
            cell.setCellValue(dto.getBnsAmt());
		}
		
		// 相关的响应信息
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		String fileName = new String("产品费用与收益导出".getBytes("GBK"), "ISO8859-1");
		response.setHeader("Content-Disposition", "Attachment;filename="
				+ fileName + "(" + curDateStr + ").xls");
		ServletOutputStream output = response.getOutputStream();
		wb.write(output);
		if (output != null) {
			output.close();
		}
	
		
	}
}
